package cn.iocoder.yudao.module.infra.controller.admin.fileClassify;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.infra.controller.admin.fileClassify.vo.FileClassifyPageReqVO;
import cn.iocoder.yudao.module.infra.controller.admin.fileClassify.vo.FileClassifyRespVO;
import cn.iocoder.yudao.module.infra.controller.admin.fileClassify.vo.FileClassifySaveReqVO;
import cn.iocoder.yudao.module.infra.dal.dataobject.fileClassify.FileClassifyDO;
import cn.iocoder.yudao.module.infra.service.fileClassify.FileClassifyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 文件分组")
@RestController
@RequestMapping("/infra/file-classify")
@Validated
public class FileClassifyController {

    @Resource
    private FileClassifyService fileClassifyService;

    @PostMapping("/create")
    @Operation(summary = "创建文件分组")
    @PreAuthorize("@ss.hasPermission('infra:file-classify:create')")
    public CommonResult<Long> createFileClassify(@Valid @RequestBody FileClassifySaveReqVO createReqVO) {
        return success(fileClassifyService.createFileClassify(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文件分组")
    @PreAuthorize("@ss.hasPermission('infra:file-classify:update')")
    public CommonResult<Boolean> updateFileClassify(@Valid @RequestBody FileClassifySaveReqVO updateReqVO) {
        fileClassifyService.updateFileClassify(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文件分组")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('infra:file-classify:delete')")
    public CommonResult<Boolean> deleteFileClassify(@RequestParam("id") Long id) {
        fileClassifyService.deleteFileClassify(id);
        return success(true);
    }

//    @DeleteMapping("/delete-list")
//    @Parameter(name = "ids", description = "编号", required = true)
//    @Operation(summary = "批量删除文件分组")
//                @PreAuthorize("@ss.hasPermission('infra:file-classify:delete')")
//    public CommonResult<Boolean> deleteFileClassifyList(@RequestParam("ids") List<Long> ids) {
//        fileClassifyService.deleteFileClassifyListByIds(ids);
//        return success(true);
//    }

    @GetMapping("/get")
    @Operation(summary = "获得文件分组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('infra:file-classify:query')")
    public CommonResult<FileClassifyRespVO> getFileClassify(@RequestParam("id") Long id) {
        FileClassifyDO fileClassify = fileClassifyService.getFileClassify(id);
        return success(BeanUtils.toBean(fileClassify, FileClassifyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文件分组分页")
    @PreAuthorize("@ss.hasPermission('infra:file-classify:query')")
    public CommonResult<PageResult<FileClassifyRespVO>> getFileClassifyPage(@Valid FileClassifyPageReqVO pageReqVO) {
        PageResult<FileClassifyDO> pageResult = fileClassifyService.getFileClassifyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FileClassifyRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得文件分组全部-精简列表")
    @PreAuthorize("@ss.hasPermission('infra:file-classify:query')")
    public CommonResult<List<FileClassifyRespVO>> getFileClassifySimple(@Valid FileClassifyPageReqVO pageReqVO) {
        List<FileClassifyDO> pageResult = fileClassifyService.getFileClassifyList(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FileClassifyRespVO.class));
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出文件分组 Excel")
//    @PreAuthorize("@ss.hasPermission('infra:file-classify:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportFileClassifyExcel(@Valid FileClassifyPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<FileClassifyDO> list = fileClassifyService.getFileClassifyPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "文件分组.xls", "数据", FileClassifyRespVO.class,
//                        BeanUtils.toBean(list, FileClassifyRespVO.class));
//    }

}
